package database.worker;

import database.DatabaseManager;
import dto.Track;
import java.util.ResourceBundle;
import java.util.concurrent.ExecutionException;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 * Worker class to load / manipulate / delete Tracks and its events in the background
 * 
 * @author Pascal
 */
public class DBTrackWorker extends SwingWorker<Object, Void> {
    
    public enum Type {
        DeleteTrack, // Track
        SaveTrack, // Track
        UpdateTrack, // Track
        GetAllTracks, // no param
        GetEventsByTrack // needs Track
    }
    
    private Type type;
    private Track track;
    private IDoneListener doneListener;
    
    /**
     * Creates a new Worker to save / delete / load / update (a) Track(s).
     * Or to load all events of a track.
     * 
     * @param type What should the worker do?
     * @param track The track to operate on (or null for loading all tracks)
     * 
     * @throws NullPointerException if type is null or track is null and type is not GetAllTracks
     */
    public DBTrackWorker(Type type, Track track) {
        if (type == null || (track == null && type != Type.GetAllTracks)) {
            throw new NullPointerException();
        }
        
        this.type = type;
        this.track = track;
        this.doneListener = null;
    }
    
    /**
     * Creates a new Worker to load all Tracks
     */
    public DBTrackWorker() {
        this(Type.GetAllTracks, null);
    }
    
    /**
     * Set a done listener to get notified when the worker finished successfully.
     * Or set to null, to remove the listener
     * 
     * @param doneListener the listener that wants to get notified
     */
    public void setDoneListener(IDoneListener doneListener) {
        this.doneListener = doneListener;
    }
    
    @Override
    protected Object doInBackground() throws Exception {        
        DatabaseManager db = DatabaseManager.getInstance();
        
        Object ret = null;
        
        switch (type) {
            case GetAllTracks:
                ret = db.getAllTracks();
                break;
            case SaveTrack:
                db.saveTrack(track);
                break;
            case DeleteTrack:
                db.deleteTrack(track);
                break;
            case UpdateTrack:
                db.updateTrack(track);
                break;
            case GetEventsByTrack:
                ret = db.getEventsByTrackId(track);
                break;
        }
        
        return ret;
    }
    
    @Override
    protected void done() {
        try {
            Object ret = get();
            
            if (doneListener != null) {
                doneListener.done(ret);
            }
        } catch (InterruptedException ex) {
        } catch (ExecutionException ex) {
            JOptionPane.showMessageDialog(null,
                    ResourceBundle.getBundle("gui/guilabels").getString("errorexecution") + ex.getLocalizedMessage(),
                    ResourceBundle.getBundle("gui/guilabels").getString("error"),
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}
